//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2007 CORE Solutions Limited
// All rights reserved.
// PROPRIETARY AND CONFIDENTIAL INFORMATION.
// Use is subject to license terms.
//
// FUNCTION ID      :   CATEGORY_ITEM_LINE
// AUTHOR           :   Ray Guo 
// DATE CREATE      :   2007/05/24
// DESCRIPTION      :   a DAO file for the function, CATEGORY_ITEM_LINE
//                  :   
//                  :   
//                  :   
//
// MODIFICATION HISTORY:
// DATE         AUTHOR          VERSION NO.         COMMENT
// -----------  --------------  ------------------  -------------------
// 2007/05/24   Ray Guo         1.0                 source is generated.
//
//
//
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.erp.app.catalogLogin.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.erp.app.common.ProjectConstants;
import com.erp.app.catalogLogin.criteria.CategoryItemLineSrchCriteria;
import com.erp.app.catalogLogin.pk.CategoryItemLinePk;
import com.erp.app.catalogLogin.pk.CategoryItemClassPk;
import com.erp.app.catalogLogin.pk.CategoryItemCategoryPk;
import com.erp.app.catalogLogin.vo.CategoryItemLineVo;
import com.erp.app.catalogLogin.vo.CategoryItemClassVo;
import com.erp.app.catalogLogin.vo.CategoryItemCategoryVo;

import com.erp.framework.exceptions.FindRecordException;
import com.erp.framework.model.criteria.SimpleBaseSrchCriteria;
import com.erp.framework.model.criteria.SimpleSearchMaintStyleSrchCriteria;
import com.erp.framework.model.dao.SimpleSearchMaintStyleDao;
import com.erp.framework.model.pk.SimpleBasePk;
import com.erp.framework.model.pk.SimpleSearchMaintStylePk;
import com.erp.framework.model.vo.ChildList;
import com.erp.framework.model.vo.SimpleBaseVo;
import com.erp.framework.model.vo.SimpleSearchMaintStyleVo;
import com.erp.framework.types.CustomBigDecimal;
import com.erp.framework.types.CustomBigInteger;
import com.erp.framework.types.CustomSqlDate;
import com.erp.framework.types.CustomTimestamp;
import com.erp.framework.types.CustomUtilDate;
import com.erp.framework.util.DateTimeUtil;
import com.erp.framework.util.NumberUtil;
import com.erp.framework.util.CommonUtil;
import com.erp.framework.util.logger.Logger;

import com.erp.app.catalogLogin.pk.CategoryItemLinePk;

/**
* <p>A <strong>DAO</strong>(Data Access Object) abstracts and encapsulates
* all access to the persistent store.  The DAO manages the connection with the
* data source to obtain and store data.  Also, the DAO implements
* the access mechanism required to work with the data source,
* and always provides a uniform API to its clients.
*
* @author          Ray Guo 
* @version 1.0     2007/05/24
* @since           FrameWork 1.0
*
*/
public class CategoryItemClassDao extends SimpleSearchMaintStyleDao {

	/**
	* Returns an array list of child VOs by the parent
	* @param         parent   the incoming parent VO
	* @param         conn     the database connection
	* @return        an array list of child VOs
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	public ChildList selectByParent(SimpleBaseVo parent, Connection conn)
		throws FindRecordException {

		return super.selectByParent(parent, CategoryItemClassVo.class, conn);
	}

	/**
	* Constructs a string of SQL insert statement
	* @return        a string of SQL insert statement
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected String sqlInsert() {

		String sql = "INSERT INTO V_CATEGORY_ITEM_CLASS ("
			+ "COMPANY_CODE, ITEM_LINE, ITEM_CATEGORY, ITEM_CLASS, ITEM_CLASS_DESC, "
			+ "STATUS, DC_IND, DC_DATE, CREATE_USER, CREATE_DATE, "
			+ "UPDATE_USER, UPDATE_DATE, SHOW_IN_CATALOG) "
			+ "VALUES ("
			+ "?, ?, ?, ?, ?, "
			+ "?, 'NO', ?, ?, SYSDATE, "
			+ "?, ?, ?)";
		return sql;
	}

	/**
	* Sets the values for a SQL insert or update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected void setValuesForInsertAndModify(PreparedStatement pStmt, Object obj, String opType)
		throws SQLException {

		CategoryItemClassVo vo = (CategoryItemClassVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
		pStmt.setString(i++, vo.getItemLine());
		pStmt.setString(i++, vo.getItemCategory());
		pStmt.setString(i++, vo.getItemClass());
		setNullableString(pStmt, i++, vo.getClassItemClassDesc());
		setNullableString(pStmt, i++, vo.getClassStatus());

		if (ProjectConstants.DB_OP_UPDATE.equals(opType))
			setNullableString(pStmt, i++, vo.getClassDcInd());
		setNullableString(pStmt, i++, vo.getClassDcDate());
		setNullableString(pStmt, i++, vo.getCreateUser());

		if (ProjectConstants.DB_OP_UPDATE.equals(opType))
			setNullableTimestamp(pStmt, i++, vo.getCreateDate());

		setNullableString(pStmt, i++, vo.getUpdateUser());

		if (ProjectConstants.DB_OP_INSERT.equals(opType))
			setNullableTimestamp(pStmt, i++, vo.getUpdateDate());
		setNullableString(pStmt, i++, vo.getClassShowInCatalog());

		if(ProjectConstants.DB_OP_UPDATE.equals(opType)){
			pStmt.setString(i++, vo.getCompanyCode());
			pStmt.setString(i++, vo.getItemLine());
			pStmt.setString(i++, vo.getItemCategory());
			pStmt.setString(i++, vo.getItemClass());
			setNullableTimestamp(pStmt, i++, vo.getUpdateDate());
		}
	}

	/**
	* Sets the values for a SQL insert statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected void setValuesForInsert(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_INSERT);
	}

	/**
	* Constructs a string of SQL update statement
	* @return        a string of SQL update statement
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected String sqlUpdate() {

		String sql = "UPDATE T_CATEGORY_ITEM_CLASS SET COMPANY_CODE=?, ITEM_LINE=?, ITEM_CATEGORY=?, ITEM_CLASS=?, "
			+ "ITEM_CLASS_DESC=?, "
			+ "STATUS=?, DC_IND=?, DC_DATE=?, CREATE_USER=?, CREATE_DATE=?, "
			+ "UPDATE_USER=?, UPDATE_DATE=SYSDATE, SHOW_IN_CATALOG=? "
			+ "WHERE COMPANY_CODE=? AND ITEM_LINE=? AND ITEM_CATEGORY=? AND ITEM_CLASS=? "
			+ "AND (UPDATE_DATE IS NULL OR UPDATE_DATE = ?) ";
		return sql;
	}

	/**
	* Sets the values for a SQL update statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected void setValuesForModify(PreparedStatement pStmt, Object obj)
		throws SQLException {

		setValuesForInsertAndModify(pStmt, obj, ProjectConstants.DB_OP_UPDATE);
	}

	/**
	* Constructs a string of SQL delete statement
	* @return        a string of SQL update statement
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected String sqlDelete() {
		String sql = "DELETE FROM T_CATEGORY_ITEM_CLASS "
				   + "WHERE COMPANY_CODE=? AND ITEM_LINE=? "
				   + "AND ITEM_CATEGORY=? AND ITEM_CLASS=? ";
		

		return sql;
	}

	/**
	* Sets the values for a SQL delete statement.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming VO
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected void setValuesForDelete(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CategoryItemClassVo vo = (CategoryItemClassVo) obj;
		int i = 1;

		pStmt.setString(i++, vo.getCompanyCode());
		pStmt.setString(i++, vo.getItemLine());
		pStmt.setString(i++, vo.getItemCategory());
		pStmt.setString(i++, vo.getItemClass());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected String sqlFind() {

		String sql = "SELECT COMPANY_CODE, ITEM_LINE, ITEM_CATEGORY, ITEM_CLASS, ITEM_CLASS_DESC, "
			+ "STATUS, DC_IND, DC_DATE, CREATE_USER, CREATE_DATE, "
			+ "UPDATE_USER, UPDATE_DATE, SHOW_IN_CATALOG "
			+ "FROM T_CATEGORY_ITEM_CLASS "
			+ "WHERE COMPANY_CODE=? AND ITEM_LINE=? AND ITEM_CATEGORY=? AND ITEM_CLASS=? "		
			+ "ORDER BY COMPANY_CODE, ITEM_LINE, ITEM_CATEGORY, ITEM_CLASS";
		return sql;
	}

	/**
	* Sets the values for a SQL select statement for finding by primary key.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming PK
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected void setValuesForFind(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CategoryItemClassPk pk = (CategoryItemClassPk) obj;
		int i = 1;

		pStmt.setString(i++, pk.getCompanyCode());
		pStmt.setString(i++, pk.getItemLine());
		pStmt.setString(i++, pk.getItemCategory());
		pStmt.setString(i++, pk.getItemClass());
	}

	/**
	* Constructs a string of SQL select statement
	* @return        a string of SQL select statement
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected String sqlList() {

		String sql = "SELECT COMPANY_CODE, ITEM_LINE, ITEM_CATEGORY, ITEM_CLASS, ITEM_CLASS_DESC, "
			+ "STATUS, DC_IND, DC_DATE, CREATE_USER, CREATE_DATE, "
			+ "UPDATE_USER, UPDATE_DATE, SHOW_IN_CATALOG "
			+ "FROM T_CATEGORY_ITEM_CLASS "
			+ "WHERE COMPANY_CODE=? AND ITEM_LINE=? AND ITEM_CATEGORY=? AND ITEM_CLASS=? "
			+ "ORDER BY COMPANY_CODE, ITEM_LINE, ITEM_CATEGORY, ITEM_CLASS";
		return sql;
	}

	/**
	* Sets the values for a SQL select statement for finding child tables.
	* @param         pStmt   the incoming prepared statement
	* @param         obj     the incoming parent PK
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected void setValuesForList(PreparedStatement pStmt, Object obj)
		throws SQLException {

		CategoryItemLinePk pk = (CategoryItemLinePk) obj;
		int i = 1;

		pStmt.setString(i++, pk.getCompanyCode());
		pStmt.setString(i++, pk.getItemLine());
//		pStmt.setString(i++, pk.getItemCategory());
//		pStmt.setString(i++, pk.getItemClass());
	}

	/**
	* Sets the values of a result set to a vo
	* @param         rs   the incoming result set
	* @return        a vo containing the result set values
	*
	* @author     Ray Guo 
	* @since      Framework 1.0
	*/
	protected SimpleBaseVo getResultSetValues(ResultSet rs) throws SQLException {

		CategoryItemClassVo vo = new CategoryItemClassVo();

		vo.setCompanyCode(rs.getString("COMPANY_CODE"));
		vo.setItemLine(rs.getString("ITEM_LINE"));
		vo.setItemCategory(rs.getString("ITEM_CATEGORY"));
		vo.setItemClass(rs.getString("ITEM_CLASS"));
		vo.setClassItemClassDesc(rs.getString("ITEM_CLASS_DESC"));
		vo.setClassStatus(rs.getString("STATUS"));
		vo.setClassDcInd(rs.getString("DC_IND"));
		vo.setClassDcDate(rs.getString("DC_DATE"));
		vo.setCreateUser(rs.getString("CREATE_USER"));
		vo.setCreateDate(rs.getTimestamp("CREATE_DATE"));
		vo.setUpdateUser(rs.getString("UPDATE_USER"));
		vo.setUpdateDate(rs.getTimestamp("UPDATE_DATE"));
		vo.setClassShowInCatalog(rs.getString("SHOW_IN_CATALOG"));

		return vo;
	}

}
